package com.sys.practise

/**
 * 基础知识
 */
object Basic {

  val name = "tom" //属性定义，属性类型可自动识别
  var address = "beijing"

  val age = (year: Int) => 2020 - year //函数定义，函数有别于方法（方法是类、对象的行为；函数是表达式）

  /**
   * 方法用def标识
   * 可以传参数，也可以不传；
   * 可以返回值，也可以不返回；
   */
  def say(content: String): String = {
    name + " say : " + content
  }

  /**
   * 练习位运算符
   */
  def operate(): Unit = {
    /* 60 = 0011 1100 */
    var a = 60;
    /* 13 = 0000 1101 */
    var b = 13;
    var c = 0;


    c = a & b; /* 12 = 0000 1100 */
    println("a & b = " + c);

    c = a | b; /* 61 = 0011 1101 */
    println("a | b = " + c);

    c = a ^ b; /* 49 = 0011 0001 */
    println("a ^ b = " + c);

    c = ~a; /* -61 = 1100 0011 */
    println("~a = " + c);

    c = a << 2; /* 240 = 1111 0000 */
    println("a << 2 = " + c);

    c = a >> 2; /* 15 = 1111 */
    println("a >> 2  = " + c);

    c = a >>> 2; /* 15 = 0000 1111 */
    println("a >>> 2 = " + c);
  }

  /**
   * 练习条件语句
   */
  def condition(color: String, go: Int): Unit = {
    if (go == 1) {
      if (color.equals("b")) {
        println("black cat")
      } else {
        println("white cat")
      }
    } else {
      if (color.equals("b")) {
        println("black cat")
      } else if (color.equals("w")) {
        println("white cat")
      } else {
        println("colorful cat")
      }
    }
  }


  /**
   * 练习循环语句
   */
  def loop(n: Int): Unit = {
    val list = Array[String]("a", "b", "c")
    n match {
      case 1 =>
        for (i <- list) {
          println(s"char is $i")
        }
      case 2 =>
        var i: Int = 0
        while (list.length > i) {
          println(s"char is ${list(i)}")
          i += 1
        }
      case _ => None
    }
  }

  /**
   * 练习集合
   */
  def collection(): Unit = {
    //List操作
    val list = List("a", "b")
    val list1 = "c" :: list //从头插入
    val list2 = list :: ("c" :: Nil) // 从尾插入，必须加上Nil,表示一个列表后添加一个新的元素成一个新的列表
    val list3 = list ::: ("c" :: Nil) // 与上一个意义不同，表示两个列表整合成一个列表
    val list4 = 1 :: (2 :: (3 :: (4 :: Nil)))
    //    list1.foreach(c => println(s"char is $c")) //输出c a b
    //    list2.foreach(c => println(s"char is $c"))  //输出list(a,b) c
    //    list3.foreach(c => println(s"char is $c")) //输出 a b c
    //    list4.foreach(n=>println(n)) //输出1 2 3 4

    //Tuple操作
    val tuple = ("a", 1)
    //    println(s"first value is ${tuple._1},second value is ${tuple._2}")
    val tuple1 = Tuple2("black cat", "white cat") //目前Scala 支持的元组为2~22
    //    println(s"first value is ${tuple1._1},second value is ${tuple1._2}")
    //    tuple.productIterator.foreach(x => println(s"value is $x")) // 转换成迭代

    //Set操作
    val set = Set(1, 2, 3, 3)
    //    set.foreach(n => println(n)) //输出 1 2 3
    val set1 = Set("!", "@", "#")
    val set2 = set ++ set1 //集合连接用++
    //    set2.foreach(x => println(x))
    val set3 = scala.collection.mutable.Set("a", "b") //可变集合才能调用add方法
    set3.add("c")

    //Map操作
    var cities: Map[String, String] = Map()
    cities += ("changsha" -> "长沙") //不可变Map不用put方法添加元素，可用+=添加元素
    cities += ("shenzhen" -> "深圳")
    //    println(cities("shenzhen"))
    val cities1 = Map("yunnan" -> "云南", "xizang" -> "西藏")
    val cities2 = cities ++ cities1 //Map之间的连接用++
    //    println(cities2("xizang"))
    val cities3: scala.collection.mutable.Map[String, String] = scala.collection.mutable.Map[String, String]()
    cities3.put("baijing", "北京") //可变Map可用put方法添加元素
    cities3.put("shanghai", "上海")
    //    println(cities3("shanghai"))

    //Option操作
    val option = Some(5, 6, 7) //有值用Some
    val option1 = None //无值用None
    //    println(s"value is ${option.get}")
    println(s"value is ${option.filter(x => x._2 == 8)}")
    //    println(s"value is ${option1.getOrElse(10)}") //没有值就给个默认值

  }

  def main(args: Array[String]) {
    //    println(say("good morning"))
    //    val x = age(2019)
    //    println(s"tom's age is $x")

    //    name="zhangsan" //val标识的常量无法重新赋值（编译不过）
    //    address="shanghai" //var标识的变量可重新赋值

    //    operate() //位运算
    //    condition("b", 2)  //条件语句练习
    //    loop(2)  //循环语句练习

    collection() //集合练习
  }

}
